package com.jmz.apt.api.controller;

import com.jmz.apt.api.Helper.ResponseHelper;
import com.jmz.apt.api.dto.impl.AdminUserDto;
import com.jmz.apt.api.dto.impl.TimeTygpGroupDto;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.entity.dbEntity.TimeTygpGroup;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.message.request.RequestMessage;
import com.jmz.apt.api.message.response.ResponseMessage;
import com.jmz.apt.api.service.TimeTplService;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class TimeTemplate {

    @Autowired
    TimeTplService timeTplService;

    @RequestMapping(value="/timeTpl",method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage getAdmins(@RequestBody RequestMessage requestMessage){
        List<TimeTygpGroup> ttgList = timeTplService.getTplGroupList(requestMessage);

        //Entities are converted to DTOs
        ModelMapper modelMapper = new ModelMapper();
        List<TimeTygpGroupDto> timeTygpGroupDtos = modelMapper.map(ttgList,new TypeToken<List<TimeTygpGroupDto>>(){}.getType());

        //Set response data
        ResponseHelper responseHelper = new ResponseHelper();
        responseHelper.setResponseDataSet("timeTemplateGroupList",timeTygpGroupDtos);
        Integer pageNo = requestMessage.getMessage().getBody().getQuery().getPageno();
        Integer pageSize = requestMessage.getMessage().getBody().getQuery().getPagesize();
        Integer recountCount = timeTygpGroupDtos.size();
        Integer pageCount = (int)Math.ceil(timeTplService.count(requestMessage) / pageSize);
        responseHelper.setResponseQuery(pageNo,pageSize,recountCount,pageCount);
        responseHelper.setResponseBCode(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc());
        ResponseMessage responseResult = responseHelper.getResponseMessageTemplate().getResponseMessage();
        return  responseResult;
    }




}
